package tankwar;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Random;

public class GamePanel extends JFrame {
    Image offScreenImage=null;
    //游戏状态 0 游戏未开始， 1 单人模式， 2 双人模式， 3 游戏暂停， 4 游戏失败， 5 游戏胜利
    public int state=0;
    private boolean start=false;
    private int a=1;
    public int count=0;
    private int width=800;
    private int height=610;
    private int enemyCount=0;
    public ArrayList<Bullet>removeList=new ArrayList<>();
    public ArrayList<Bullet>bulletList=new ArrayList<>();
    public ArrayList<Tank>tankList=new ArrayList<>();
    public ArrayList<Bot> botList=new ArrayList<>();
    public ArrayList<Wall> wallList=new ArrayList<>();
    public ArrayList<Base> baseList=new ArrayList<>();
    public ArrayList<BlastObj> blastList=new ArrayList<>();
    private Image select = Toolkit.getDefaultToolkit().getImage("images/selecttank.gif");
    private int y = 150;
    Base base = new Base("images/star.gif", 375, 565, this);
    private PlayerOne playerOne = new PlayerOne("images/player1/p1tankU.gif", 150, 510,
            "images/player1/p1tankU.gif","images/player1/p1tankD.gif",
            "images/player1/p1tankL.gif","images/player1/p1tankR.gif", this);
    private PlayerTwo playerTwo = new PlayerTwo("images/player2/p2tankU.gif", 580, 510,
            "images/player2/p2tankU.gif","images/player2/p2tankD.gif",
            "images/player2/p2tankL.gif","images/player2/p2tankR.gif", this);
    public void launch() {
        setTitle("坦克大战");
        setSize(width,height);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(3);
        setResizable(false);
        setVisible(true);
        this.addKeyListener(new GamePanel.KeyMonitor());

        for(int i = 0; i< 14; i ++){
            wallList.add(new Wall("images/walls.gif", i*60 ,170, this ));
        }
        wallList.add(new Wall("images/walls.gif", 305 ,560,this ));
        wallList.add(new Wall("images/walls.gif", 305 ,500,this ));
        wallList.add(new Wall("images/walls.gif", 365 ,500,this ));
        wallList.add(new Wall("images/walls.gif", 425 ,500,this ));
        wallList.add(new Wall("images/walls.gif", 425 ,560,this ));
        baseList.add(base);
        while(true) {
            if(botList.size()==0&&enemyCount==10) {
                state=5;
            }
            if((tankList.size()==0||baseList.size()==0)&&(state==1||state==2)) {
                state=4;
            }
            if(state==1||state==2) {
                if(count%100==1&&enemyCount<10) {
                    Random r=new Random();
                    int rnum=r.nextInt(800);
                    botList.add(new Bot("images/enemy/enemy1U.gif", rnum, 110,
                            "images/enemy/enemy1U.gif","images/enemy/enemy1D.gif",
                            "images/enemy/enemy1L.gif","images/enemy/enemy1R.gif", this));
                    enemyCount++;
                }
            }
            repaint();
            try {
                Thread.sleep(25);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void paint(Graphics g) {
        if(offScreenImage==null) {
            offScreenImage=this.createImage(width,height);
        }
        Graphics gImage=offScreenImage.getGraphics();
        gImage.setColor(Color.gray);
        gImage.fillRect(0,0,width,height);
        gImage.setColor(Color.BLUE);
        gImage.setFont(new Font("仿宋",Font.BOLD,50));
        if(state==0) {
            gImage.drawString("选择游戏模式",220,100);
            gImage.drawString("单人游戏",220,200);
            gImage.drawString("双人游戏",220,300);
            gImage.drawString("按1，2选择模式，按回车开始游戏",0,400);
            gImage.drawImage(select,160,y,null);
        }else if(state==1||state==2) {
            gImage.setColor(Color.CYAN);
            gImage.setFont(new Font("宋体",Font.BOLD,20));
            gImage.drawString("剩余敌人：" + botList.size(), 10, 60);
            gImage.setColor(Color.black);
            gImage.drawString("WASD控制移动",10,550);
            gImage.drawString("空格射击",10,590);
            if(state == 2){
                gImage.drawString("方向键控制移动",640,550);
                gImage.drawString("1射击",730,590);
            }

            //paint重绘游戏元素
            for(Tank tank : tankList){
                tank.paintSelf(gImage);
            }
            for(Bullet bullet: bulletList){
                bullet.paintSelf(gImage);
            }
            bulletList.removeAll(removeList);
            for(Bot bot: botList){
                bot.paintSelf(gImage);
            }
            for (Wall wall: wallList){
                wall.paintSelf(gImage);
            }
            for(Base base : baseList){
                base.paintSelf(gImage);
            }
            for(BlastObj blast : blastList){
                blast.paintSelf(gImage);
            }
            //重绘次数+1
            count++;
        }
        else if(state == 3){
            gImage.drawString("游戏暂停",220,200);
        }
        else if(state == 4){
            gImage.drawString("游戏失败",220,200);
        }
        else if(state == 5){
            gImage.drawString("游戏胜利",220,200);
        }
        /** 将缓冲区绘制好的图形整个绘制到容器的画布中 */
        g.drawImage(offScreenImage, 0, 0, null);
    }

    private class KeyMonitor extends KeyAdapter {
        @Override
        public void keyPressed(KeyEvent e) {
            int key=e.getKeyCode();
            switch (key) {
                case KeyEvent.VK_1:
                    if(!start) {
                        y=150;
                        a=1;
                    }
                    System.out.println(state);
                    break;
                case KeyEvent.VK_2:
                    if(!start) {
                        y=250;
                        a=2;
                    }
                    System.out.println(state);
                    break;
                case KeyEvent.VK_ENTER:
                    state=a;
                    if(state==1&&!start) {
                        tankList.add(playerOne);
                    }else {
                        tankList.add(playerOne);
                        tankList.add(playerTwo);
                    }
                    start=true;
                    System.out.println("state:"+" "+state);
                    break;
                case KeyEvent.VK_P:
                    if(state!=3) {
                        a=state;
                        state=3;
                    }else{
                        state=a;
                        if(a==0) {
                            a=1;
                        }
                    }
                    break;
                default:
                    playerOne.keyPressed(e);
                    playerTwo.keyPressed(e);
                    break;
            }
        }
        public void keyReleased(KeyEvent e) {
            playerOne.keyReleased(e);
            playerTwo.keyReleased(e);
        }
    }
    public static void main(String[]args) {
        GamePanel gamePanel=new GamePanel();
        gamePanel.launch();
    }
}
